﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using MySql.Data.MySqlClient;
using CodeRobot.Utility;
using System.Windows.Forms;

namespace CodeRobot.DAL
{
    /// <summary>
    /// .net core处理请求类
    /// </summary>
    public class ViewMutiEditHelper
    {

        /// <summary>
        /// 根据表名创建Edit编辑页面
        /// </summary>
        /// <param name="strFilePath">保存路径</param>
        /// <param name="strProjectName">项目名</param>
        /// <param name="strTableName">表名</param>
        public static void CreateViewMutiEditClass(string strFilePath,string strProjectName, string strTableName,string strTableComment, string strDBName)
        {
            //读取版权信息
            CodeRobot.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\config.ini");
            string strCompany = iniFile.GetString("COPYRIGHT", "COMPANY", "");
            string strAuthor = iniFile.GetString("COPYRIGHT", "AUTHOR", "");
            string strVersion = iniFile.GetString("COPYRIGHT", "VERSION", "");
            string strCode = iniFile.GetString("COPYRIGHT", "CODE", "");
            string strCreateDate = iniFile.GetString("BASE", "CREATE_DATE", "");
            string strTablePrefix = iniFile.GetString("BASE", "TABLE_PREFIX", "");
            string strTableNameUpper = CommonHelper.GetTableNameUpper(strTableName);
            
            string strPrimaryKey = CommonHelper.GetPrimaryKey(strTableName);//如：news_id

            string strAllColumnName = CommonHelper.GetAllColumnName(strTableName);
            string strAllColumnNameNotKey = CommonHelper.GetAllColumnNameNotKey(strTableName);

            string strClassName = CommonHelper.GetClassName(strTableName);//类名
            strClassName = CommonHelper.GetTableNameUpper(strClassName);
            string strTableNameSpec = CommonHelper.GetTableNameFirstLowerSecondUpper(strClassName);//如：newsTypes
            string strTableNameLower = strTableNameSpec.ToLower();//如：newstypes

            Directory.CreateDirectory(strFilePath);
            //为每个表创建一个独立目录存放
            string strTableNamePath = strFilePath + "\\"+ strClassName;
            if (!Directory.Exists(strTableNamePath))
            {
                Directory.CreateDirectory(strTableNamePath);
            }
            StreamWriter sw = new StreamWriter(strTableNamePath + "\\Edit.cshtml", false, Encoding.GetEncoding("utf-8"));//生成物理文件

            string strTableComment2 = CommonHelper.GetColumnKeyComment(strTableComment);
            string strRichEdit = GetRichEditColumnName(strDBName, strTableName);

            sw.WriteLine("@{");
            sw.WriteLine("    ViewData[\"Title\"] = \"编辑"+ strTableComment2 + "\";");
            sw.WriteLine("    Layout = \"~/Views/Shared/_LayoutNone.cshtml\";");
            sw.WriteLine("}");
            sw.WriteLine("<div class=\"layui-fluid\">");
            sw.WriteLine("    <div class=\"layui-row layui-col-space15\">");
            sw.WriteLine("        <div class=\"layui-col-md12\">");
            sw.WriteLine("            <div class=\"layui-card\">");
            sw.WriteLine("                <div class=\"layui-card-body\" pad15>");
            sw.WriteLine("                    <div class=\"layui-form\" lay-filter=\"layuiadmin-app-form-list\" id=\"layuiadmin-app-form-list\" style=\"padding: 20px 30px 0 0;\">");
            sw.WriteLine(CommonHelper.GetEditItemList(strDBName, strTableName));
            sw.WriteLine("                        <div class=\"layui-form-item\">");
            sw.WriteLine("                            <label class=\"layui-form-label\"></label>");
            sw.WriteLine("                            <div class=\"layui-input-inline\">");
            sw.WriteLine("                                <input type=\"hidden\" name=\"" + strPrimaryKey + "\" id=\"" + strPrimaryKey + "\" value=\"@ViewContext.HttpContext.Request.Query[\"id\"]\" />");
            sw.WriteLine(GetUploadImgHiddenInput(strDBName, strTableName));
            // sw.WriteLine(GetBoolHiddenInput(strDBName, strTableName));
            // sw.WriteLine("            <input type=\"hidden\" id=\"created_at\" name=\"created_at\" value=\"@DateTime.Now.ToString()\" />");
            // sw.WriteLine("            <input type=\"hidden\" name=\"" + GetSwitchStatusKey(strDBName, strTableName) + "\" id=\"" + GetSwitchStatusKey(strDBName, strTableName) + "\" value=\"true\" />");
            sw.WriteLine("                                <input type=\"button\" class=\"layui-btn\" lay-submit lay-filter=\"layuiadmin-app-form-edit\" id=\"layuiadmin-app-form-edit\" value=\"确定保存\">");
            sw.WriteLine("                            </div>");
            sw.WriteLine("                        </div>");
            sw.WriteLine("                    </div>");
            sw.WriteLine("                </div>");
            sw.WriteLine("            </div>");
            sw.WriteLine("        </div>");
            sw.WriteLine("    </div>");
            sw.WriteLine("</div>");
            sw.WriteLine("");
            sw.WriteLine("<script src=\"~/layuiadmin/layui/layui.js\"></script>");
            sw.WriteLine("<script>");
            sw.WriteLine("    layui.config({");
            sw.WriteLine("        base: '/layuiadmin/' // 静态资源所在路径");
            sw.WriteLine("    }).extend({");
            sw.WriteLine("        index: '/lib/index' // 主入口模块");
            sw.WriteLine("    }).use(['index', 'layedit', 'upload', 'form'], function () {");
            sw.WriteLine("        var $ = layui.$");
            sw.WriteLine("            , form = layui.form");
            sw.WriteLine("            , admin = layui.admin");
            sw.WriteLine("            , upload = layui.upload");
            sw.WriteLine("            , layedit = layui.layedit;");
            sw.WriteLine("");
            if (!string.IsNullOrEmpty(strRichEdit))
            {
                sw.WriteLine("        var index = layedit.build('edit_content', {");
                sw.WriteLine("            height: 300");
                sw.WriteLine("            , uploadImage: {");
                sw.WriteLine("                multiple: true,");
                sw.WriteLine("                url: \"/upload/uploadimg?catalogue=service\"");
                sw.WriteLine("                , type: 'post'");
                sw.WriteLine("                , headers: {");
                sw.WriteLine("                    'RequestVerificationToken': csrfToken");
                sw.WriteLine("                }");
                sw.WriteLine("            }");
                sw.WriteLine("        }); //建立编辑器");
            }
            sw.WriteLine("");
            sw.WriteLine(GetUploadImgScript(strProjectName, strDBName, strTableName));
            sw.WriteLine("        // 默认加载");
            sw.WriteLine("        $(function () {");
            sw.WriteLine("            var "+strPrimaryKey+ " = $(\"#" + strPrimaryKey + "\").val();");
            // sw.WriteLine("");
            // sw.WriteLine("            // 读取数据加载loading..");
            sw.WriteLine("            loading = layer.load(2, {");
            sw.WriteLine("                shade: [0.2, '#000']");
            sw.WriteLine("            });");
            sw.WriteLine("");
            sw.WriteLine("            admin.req({");
            sw.WriteLine("                url: '/" + strTableNameLower + "/details/?id='+ " + strPrimaryKey + "");
            sw.WriteLine("                , data: null");
            sw.WriteLine("                , done: function (res) {");
            sw.WriteLine("                    // console.log(`res=${JSON.stringify(res)}`)");
            sw.WriteLine("                    layer.close(loading);");
            sw.WriteLine(CommonHelper.GetEditItemValueList(strProjectName, strDBName, strTableName));
            // sw.WriteLine("                    //if (!res.data." + GetSwitchStatusKey(strDBName, strTableName) + ") {");
            // sw.WriteLine("                        //$(\".layui-unselect\").removeClass(\"layui-form-onswitch\");");
            // sw.WriteLine("                        //$(\"#switch_"+ GetSwitchStatusKey(strDBName, strTableName) + "\").removeAttr(\"checked\");");
            // sw.WriteLine("                        //$(\".layui-unselect>em\").html(\"禁用\");");
            // sw.WriteLine("                    //}");
            if (!string.IsNullOrEmpty(strRichEdit))
            {
                sw.WriteLine("                    layedit.setContent(index, res.data."+ strRichEdit + ");");
            }
            sw.WriteLine("                    form.render();");
            sw.WriteLine(CommonHelper.GetSelectScriptForEdit(strProjectName, strDBName, strTableName, strTablePrefix));
            sw.WriteLine("");
            sw.WriteLine("                }");
            sw.WriteLine("            });");
            sw.WriteLine("");
            sw.WriteLine("        });");
            sw.WriteLine("");
            sw.WriteLine(GetSwitchScript(strDBName, strTableName));
            sw.WriteLine("");
            sw.WriteLine(GetAreaScriptListen(strProjectName, strDBName, strTableName));
            sw.WriteLine("        // 监听提交");
            sw.WriteLine("        form.on('submit(layuiadmin-app-form-edit)', function (data) {");
            if (!string.IsNullOrEmpty(strRichEdit))
            {
                sw.WriteLine("            data.field." + GetEditorKey(strDBName, strTableName) + " = layedit.getContent(index);");
            }
            sw.WriteLine("            loading = layer.load(2, {");
            sw.WriteLine("                shade: [0.2, '#000']");
            sw.WriteLine("            });");
            sw.WriteLine("");
            sw.WriteLine("            // 提交数据");
            sw.WriteLine("            admin.req({");
            sw.WriteLine("                headers: {");
            sw.WriteLine("                    'RequestVerificationToken': csrfToken");
            sw.WriteLine("                },");
            sw.WriteLine("                method: 'POST',");
            sw.WriteLine("                url: '/" + strTableNameLower + "/edit/' + data.field." + strPrimaryKey + "");
            sw.WriteLine("                , data: data.field");
            sw.WriteLine("                , done: function (res) {");
            sw.WriteLine("                    // console.log(`res=${JSON.stringify(res)}`)");
            sw.WriteLine("                    layer.close(loading);");
            sw.WriteLine("                    if(res.errcode){");
            sw.WriteLine("                        layer.open({");
            sw.WriteLine("                          title: '错误提示'");
            sw.WriteLine("                          ,type: 1");
            sw.WriteLine("                           ,content: '<div style=\"color: red; padding: 20px; \">'+ res.errcode +':'+ res.errmsg +'</div>'");
            sw.WriteLine("                          ,btn: [\"确定\"]");
            sw.WriteLine("                        });");
            sw.WriteLine("                    } else {");
            sw.WriteLine("                        layer.msg(res.msg, {");
            sw.WriteLine("                            offset: '15px'");
            sw.WriteLine("                            , icon: 1");
            sw.WriteLine("                            , time: 2000");
            sw.WriteLine("                        }, function () {");
            sw.WriteLine("                            window.location.reload();");
            sw.WriteLine("                        });");
            sw.WriteLine("                    }");
            sw.WriteLine("                }");
            sw.WriteLine("            });");
            sw.WriteLine("        });");
            sw.WriteLine("    })");
            sw.WriteLine("</script>");
            sw.Close();

        }

        /// <summary>
        /// 获取监听开关脚本
        /// </summary>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        public static string GetSwitchScript(string strDBName, string strTableName)
        {
            string strReturnValue = "";

            try
            {
                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT COLUMN_NAME,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT,EXTRA,COLUMN_DEFAULT,CHARACTER_SET_NAME FROM `information_schema`.`COLUMNS` WHERE TABLE_SCHEMA='" + strDBName + "' AND TABLE_NAME='" + strTableName + "' ORDER BY ordinal_position ";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strColumnName = dr["COLUMN_NAME"].ToString();//字段列名
                    string strColumnKey = dr["COLUMN_KEY"].ToString();//是否是主键
                    string strColumnComment = dr["COLUMN_COMMENT"].ToString();//注释


                    if (strColumnComment.Contains("{勾选}"))
                    {
                        string strColumnComment2 = CommonHelper.GetColumnKeyComment(strColumnComment);

                        string strValue = "        // 监听指定开关\r\n";
                        strValue += "        form.on('switch(switch_"+ strColumnName + ")', function (data) {\r\n";
                        strValue += "            if (this.checked) {\r\n";
                        strValue += "                $(\"#" + strColumnName + "\").val(true);\r\n";
                        strValue += "            } else {\r\n";
                        strValue += "                $(\"#" + strColumnName + "\").val(false);\r\n";
                        strValue += "            }\r\n";
                        strValue += "        });\r\n";

                        strReturnValue += strValue;
                    }

                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(CodeHelper), ex, "获取监听开关脚本", "GetSwitchScript", false);
            }

            return strReturnValue;

        }

        /// <summary>
        /// 获取上传图片脚本
        /// </summary>
        /// <param name="strProjectName"></param>
        /// <param name="strDBName"></param>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        public static string GetUploadImgScript(string strProjectName, string strDBName, string strTableName)
        {
            string strReturnValue = "";

            try
            {
                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT COLUMN_NAME,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT,EXTRA,COLUMN_DEFAULT,CHARACTER_SET_NAME FROM `information_schema`.`COLUMNS` WHERE TABLE_SCHEMA='" + strDBName + "' AND TABLE_NAME='" + strTableName + "' ORDER BY ordinal_position ";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strColumnName = dr["COLUMN_NAME"].ToString();//字段列名
                    string strColumnKey = dr["COLUMN_KEY"].ToString();//是否是主键
                    string strColumnComment = dr["COLUMN_COMMENT"].ToString();//注释


                    if (strColumnComment.Contains("{图片}"))
                    {
                        string strColumnComment2 = CommonHelper.GetColumnKeyComment(strColumnComment);

                        string strValue = "        // 上传图片至服务端\r\n";
                        strValue += "        upload.render({\r\n";
                        strValue += "            elem: '#cover-upload-drag'\r\n";
                        strValue += "            ,headers: {\r\n";
                        strValue += "                'RequestVerificationToken': csrfToken\r\n";
                        strValue += "            }\r\n";
                        strValue += "            , url: '/upload/uploadimg?catalogue=service&open_breviary=true'\r\n";
                        strValue += "            , done: function (res) {\r\n";
                        strValue += "                // console.log(`res=${JSON.stringify(res)}`);\r\n";
                        strValue += "                if (res.code > 0) {\r\n";
                        strValue += "                    return layer.msg('上传失败');\r\n";
                        strValue += "                }\r\n";
                        strValue += "                $(\"#cover-upload-drag\").html('<img style=\"max-width:200px;max-height:150px\" src=\"' + res.data.src +'\">');\r\n";
                        strValue += "                $(\"#" + strColumnName + "\").val(res.path);\r\n";
                        strValue += "            }\r\n";
                        strValue += "        });\r\n";

                        strReturnValue += strValue;
                    }

                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(CodeHelper), ex, "获取搜索字段列表", "GetSearchItemList", false);
            }

            return strReturnValue;

        }

        /// <summary>
        /// 获取上传图片隐藏输入框
        /// </summary>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        public static string GetUploadImgHiddenInput(string strDBName, string strTableName)
        {
            string strReturnValue = "";

            try
            {
                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT COLUMN_NAME,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT,EXTRA,COLUMN_DEFAULT,CHARACTER_SET_NAME FROM `information_schema`.`COLUMNS` WHERE TABLE_SCHEMA='" + strDBName + "' AND TABLE_NAME='" + strTableName + "' ORDER BY ordinal_position ";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strColumnName = dr["COLUMN_NAME"].ToString();//字段列名
                    string strColumnKey = dr["COLUMN_KEY"].ToString();//是否是主键
                    string strColumnComment = dr["COLUMN_COMMENT"].ToString();//注释

                    if (strColumnComment.Contains("{图片}") || strColumnComment.Contains("头像") || strColumnName.Contains("avatar") || strColumnName.Contains("logo") || strColumnName.Contains("img") || strColumnName.Contains("path"))
                    {
                        string strColumnComment2 = CommonHelper.GetColumnKeyComment(strColumnComment);

                        string strValue = "                                <input type=\"hidden\" id=\"" + strColumnName + "\" name=\"" + strColumnName + "\" value=\"\" />";
                        strReturnValue += strValue;
                    }

                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(CodeHelper), ex, "获取搜索字段列表", "GetSearchItemList", false);
            }

            return strReturnValue;

        }

        /// <summary>
        /// 获取布尔型隐藏输入框
        /// </summary>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        public static string GetBoolHiddenInput(string strDBName, string strTableName)
        {
            string strReturnValue = "";

            try
            {
                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT COLUMN_NAME,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT,EXTRA,COLUMN_DEFAULT,CHARACTER_SET_NAME FROM `information_schema`.`COLUMNS` WHERE TABLE_SCHEMA='" + strDBName + "' AND TABLE_NAME='" + strTableName + "' ORDER BY ordinal_position ";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strColumnName = dr["COLUMN_NAME"].ToString();//字段列名
                    string strColumnKey = dr["COLUMN_KEY"].ToString();//是否是主键
                    string strColumnComment = dr["COLUMN_COMMENT"].ToString();//注释

                    if (strColumnComment.Contains("{勾选}"))
                    {
                        string strColumnComment2 = CommonHelper.GetColumnKeyComment(strColumnComment);

                        string strValue = "            <input type=\"hidden\" id=\"" + strColumnName + "\" name=\"" + strColumnName + "\" value=\"true\" />\r\n";
                        strReturnValue += strValue;
                    }

                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(CodeHelper), ex, "获取搜索字段列表", "GetSearchItemList", false);
            }

            return strReturnValue;

        }

        /// <summary>
        /// 获取富文本编辑器字段
        /// </summary>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        public static string GetEditorKey(string strDBName, string strTableName)
        {
            string strReturnValue = "";

            try
            {
                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT COLUMN_NAME,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT,EXTRA,COLUMN_DEFAULT,CHARACTER_SET_NAME FROM `information_schema`.`COLUMNS` WHERE TABLE_SCHEMA='" + strDBName + "' AND TABLE_NAME='" + strTableName + "' ORDER BY ordinal_position ";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strColumnName = dr["COLUMN_NAME"].ToString();//字段列名
                    string strColumnKey = dr["COLUMN_KEY"].ToString();//是否是主键
                    string strColumnComment = dr["COLUMN_COMMENT"].ToString();//注释
                    string strColumnType = dr["COLUMN_TYPE"].ToString();//注释

                    if (strColumnType== "longtext")
                    {
                        strReturnValue = strColumnName;
                    }

                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(CodeHelper), ex, "获取搜索字段列表", "GetSearchItemList", false);
            }

            return strReturnValue;

        }

        /// <summary>
        /// 获取状态开关字段
        /// </summary>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        public static string GetSwitchStatusKey(string strDBName, string strTableName)
        {
            string strReturnValue = "";

            try
            {
                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT COLUMN_NAME,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT,EXTRA,COLUMN_DEFAULT,CHARACTER_SET_NAME FROM `information_schema`.`COLUMNS` WHERE TABLE_SCHEMA='" + strDBName + "' AND TABLE_NAME='" + strTableName + "' ORDER BY ordinal_position ";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strColumnName = dr["COLUMN_NAME"].ToString();//字段列名
                    string strColumnKey = dr["COLUMN_KEY"].ToString();//是否是主键
                    string strColumnComment = dr["COLUMN_COMMENT"].ToString();//注释
                    string strColumnType = dr["COLUMN_TYPE"].ToString();//注释

                    if (strColumnComment.Contains("{勾选}"))
                    {
                        strReturnValue = strColumnName;
                    }

                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(CodeHelper), ex, "获取状态开关字段", "GetSwitchStatusKey", false);
            }

            return strReturnValue;

        }

        /// <summary>
        /// 获取监听省市区选择脚本
        /// </summary>
        /// <param name="strProjectName"></param>
        /// <param name="strDBName"></param>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        public static string GetAreaScriptListen(string strProjectName, string strDBName, string strTableName)
        {
            string strReturnValue = "";

            try
            {
                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT COLUMN_NAME,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT,EXTRA,COLUMN_DEFAULT,CHARACTER_SET_NAME FROM `information_schema`.`COLUMNS` WHERE TABLE_SCHEMA='" + strDBName + "' AND TABLE_NAME='" + strTableName + "' ORDER BY ordinal_position ";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strColumnName = dr["COLUMN_NAME"].ToString();//字段列名
                    string strColumnKey = dr["COLUMN_KEY"].ToString();//是否是主键
                    string strColumnComment = dr["COLUMN_COMMENT"].ToString();//注释

                    if (strColumnComment.Contains("省"))
                    {
                        string strColumnComment2 = CommonHelper.GetColumnKeyComment(strColumnComment);

                        string strValue = "        // 监听省份选择\r\n";
                        strValue += "        form.on('select(select_province_id)', function (data) {\r\n";
                        strValue += "            $(\"#city_id\").html('<option value=\"\">选择市</option>');\r\n";
                        strValue += "            // 选择市\r\n";
                        strValue += "            admin.req({\r\n";
                        strValue += "                url: '/areas/list?parent_id=' + data.value\r\n";
                        strValue += "                , done: function (res) {\r\n";
                        strValue += "                    layer.close(loading);\r\n";
                        strValue += "                    $.each(res.data, function (i, item) {\r\n";
                        strValue += "                        $(\"#city_id\").append(\"<option value=\\\"\" + res.data[i].area_id + \"\\\" >\" + res.data[i].area_name + \"</option>\");\r\n";
                        strValue += "                    });\r\n";
                        strValue += "                    form.render();\r\n";
                        strValue += "                }\r\n";
                        strValue += "            });\r\n";
                        strValue += "        });\r\n\r\n";
                        strValue += "        // 监听市选择\r\n";
                        strValue += "        form.on('select(select_city_id)', function (data) {\r\n";
                        strValue += "            $(\"#district_id\").html('<option value=\"\">选择区</option>');\r\n";
                        strValue += "            // 选择区\r\n";
                        strValue += "            admin.req({\r\n";
                        strValue += "                url: '/areas/list?parent_id=' + data.value\r\n";
                        strValue += "                , done: function (res) {\r\n";
                        strValue += "                    layer.close(loading);\r\n";
                        strValue += "                    $.each(res.data, function (i, item) {\r\n";
                        strValue += "                        $(\"#district_id\").append(\"<option value=\\\"\" + res.data[i].area_id + \"\\\" >\" + res.data[i].area_name + \"</option>\");\r\n";
                        strValue += "                    });\r\n";
                        strValue += "                    form.render();\r\n";
                        strValue += "                }\r\n";
                        strValue += "            });\r\n";
                        strValue += "        });\r\n";

                        strReturnValue += strValue;
                    }

                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(CodeHelper), ex, "获取搜索字段列表", "GetSearchItemList", false);
            }

            return strReturnValue;

        }

        /// <summary>
        /// 获取富文本编辑器
        /// </summary>
        /// <param name="strDBName"></param>
        /// <param name="strTableName"></param>
        /// <returns></returns>
        public static string GetRichEditColumnName(string strDBName, string strTableName)
        {
            string strReturnValue = "";

            try
            {
                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT COLUMN_NAME,DATA_TYPE,COLUMN_TYPE,COLUMN_KEY,COLUMN_COMMENT,EXTRA,COLUMN_DEFAULT,CHARACTER_SET_NAME FROM `information_schema`.`COLUMNS` WHERE TABLE_SCHEMA='" + strDBName + "' AND TABLE_NAME='" + strTableName + "' ORDER BY ordinal_position ";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strColumnName = dr["COLUMN_NAME"].ToString();//字段列名
                    string strColumnKey = dr["COLUMN_KEY"].ToString();//是否是主键
                    string strColumnComment = dr["COLUMN_COMMENT"].ToString();//注释
                    string strDataType = dr["DATA_TYPE"].ToString();//数据类型

                    if (strColumnComment.Contains("{富文本}") || strDataType.Contains("text") && strColumnName.Contains("intro"))
                    {
                        strReturnValue = strColumnName;
                    }
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(CodeHelper), ex, "获取上传图片字段", "GetImgColumnName", false);
            }

            return strReturnValue;

        }
    }
}
